package com.zfk.framework.casefield_validator;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.zfk.framework.models.CaseData;

import static com.zfk.framework.casefield_validator.ValidatorCaseField.*;

public class CaseValidator {
    //日志处理
    private static final Logger logger = LogManager.getLogger(CaseValidator.class);
    public static final Set<String> VALID_METHODS = new HashSet<>();
    public static final Set<String> VALID_REQUEST_TYPES = new HashSet<>();

    //  使用静态代码块，给类变量传值
    static {
        VALID_METHODS.add("get");
        VALID_METHODS.add("post");
        VALID_METHODS.add("patch");
        VALID_METHODS.add("delete");
        VALID_METHODS.add("head");
        VALID_METHODS.add("option");
        VALID_REQUEST_TYPES.add("params");
        VALID_REQUEST_TYPES.add("form");
        VALID_REQUEST_TYPES.add("json");
        VALID_REQUEST_TYPES.add("file");
        VALID_REQUEST_TYPES.add("None");
    }

    /**
     * 校验用例数据
     * @param caseId 用例编号
     * @param caseDataMap 用例数据
     * @param filePath 文件路径
     * @return 用例对象
     * 抛出异常则说明用例数据不合法
     * 先校验必填字段，然后验证合法值，再验证Map字段，最后构建用例对象
     */
    public static CaseData validateAndParseCase(String caseId, Map<String, Object> caseDataMap, String filePath) {
        logger.info("开始校验测试用例{}",caseId);
        //这里说明一下，单条测试用例的类型为Map<caseId,caseData>,

        // 先校验必填字段：url, method, detail, requestType
        validateStringField(caseId, "url", filePath, caseDataMap);
        validateStringField(caseId, "method", filePath, caseDataMap);
        validateStringField(caseId, "detail", filePath, caseDataMap);
        validateStringField(caseId, "requestType", filePath, caseDataMap);

        //再验证请求方式的合法性
        String method = ((String) caseDataMap.get("method")).toLowerCase();
        if (!VALID_METHODS.contains(method)) {
            logger.error("文件{}中用例 {} 请求方式 {} 不合法，请检查！",filePath,  caseId, method);
            throw new IllegalArgumentException("文件"+filePath+"中用例 " + caseId + " 请求方式 " + method + " 不合法！");
        }
        //再验证传参方式的合法性
        String requestType = ((String) caseDataMap.get("requestType")).toLowerCase();
        if (!VALID_REQUEST_TYPES.contains(requestType)) {
            logger.error("文件{}中用例 {} 传参方式 {} 不合法，请检查！",filePath,  caseId, requestType);
            throw new IllegalArgumentException("文件"+filePath+"中用例 " + caseId + " 传参方式 " + requestType + " 不合法！");
        }
        //还需要校验Map字段
        validateMapField(caseId, "headers", filePath, caseDataMap);
        validateOptionalMapField(caseId, "data", filePath, caseDataMap);
        validateExpectedField(caseId, "expected", filePath, caseDataMap);
        validateExtractField(caseId, "extract", filePath, caseDataMap);

        //执行完校验逻辑，开始构建用例对象
        CaseData caseData = new CaseData();
        caseData.setUrl((String) caseDataMap.get("url"));
        caseData.setMethod((String) caseDataMap.get("method"));
        caseData.setDetail((String) caseDataMap.get("detail"));
        caseData.setRequestType((String) caseDataMap.get("requestType"));
        caseData.setHeaders(getMapFieldValue("headers", caseDataMap));
        caseData.setData(getMapFieldValue("data", caseDataMap));
        caseData.setExpected(getMapFieldValue("expected", caseDataMap));
        caseData.setExtract(getMapFieldValue("extract", caseDataMap));

        logger.info("文件{}中用例{}校验通过", filePath, caseId);

        return caseData;
    }

}